package com.yisu.expressway.onedollar.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.yisu.expressway.R;
import com.yisu.expressway.onedollar.adapter.NumGridListAdapter;
import com.yisu.expressway.onedollar.widget.TitleView;
import com.yisu.expressway.ui.recyclerview.BaseRvAdapter;
import com.yisu.expressway.ui.recyclerview.DividerListItemDecoration;
import com.yisu.expressway.ui.recyclerview.PagingRecyclerView;
import com.yisu.expressway.utils.SpannableUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;

/**
 * Created by shijunfeng on 2017/1/2.
 */

public class LuckyNumDetailActivity extends BasicActivity {
    private static final String EXTRA_KEY_LUCKY_NUM = "extra_key_lucky_num";
    private static final String EXTRA_KEY_GOODS_NAME = "extra_key_goods_name";
    private static final String EXTRA_KEY_PERIODS_NUM = "extra_key_periods_num";
    @Bind(R.id.title_bar)
    protected TitleView mTitleView;
    @Bind(R.id.tv_goods_name)
    protected TextView mGoodsNameTv;
    @Bind(R.id.tv_periodsnum)
    protected TextView mPeriodsNumTv;
    @Bind(R.id.tv_lucknum_des)
    protected TextView mLuckyNumDesTv;
    @Bind(R.id.gridview)
    protected GridView mGridView;

    @Override
    protected int getThemeId() {
        return R.style.OneDollarTheme;
    }

    @Override
    protected int getInflateLayoutId() {
        return R.layout.activity_one_dollar_lucky_num_detail;
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected void releasePresenter() {

    }
    private List<String> mLuckyNumList = new ArrayList<>();
    private String mGoodsName;
    private long mPeriodsNum;
    @Override
    protected void getIntentData(Intent intent) {
        String stringExtra = intent.getStringExtra(EXTRA_KEY_LUCKY_NUM);
        if (TextUtils.isEmpty(stringExtra)){
            return;
        }
        String[] split = stringExtra.split(",");
        mLuckyNumList = Arrays.asList(split);
        mGoodsName = intent.getStringExtra(EXTRA_KEY_GOODS_NAME);
        mPeriodsNum = intent.getLongExtra(EXTRA_KEY_PERIODS_NUM, -1);
    }

    @Override
    protected void getSavedBundleData(Bundle bundle) {

    }

    @Override
    protected void initViews() {
        NumGridListAdapter numGridListAdapter = new NumGridListAdapter(this, mLuckyNumList);
        mGridView.setAdapter(numGridListAdapter);
        numGridListAdapter.notifyDataSetChanged();
        if (!TextUtils.isEmpty(mGoodsName)){
            mGoodsNameTv.setText(mGoodsName);
        }
        if (mPeriodsNum > 0){
            String format = String.format(getString(R.string.one_dollar_periods_num), mPeriodsNum);
            mPeriodsNumTv.setText(format);
        }

        String format = String.format(getString(R.string.one_dollar_luck_num_des), mLuckyNumList != null ? mLuckyNumList.size() : 0);
        mLuckyNumDesTv.setText(SpannableUtil.setHighLight(this,format, Integer.toString(mLuckyNumList != null ? mLuckyNumList.size() : 0),R.color.oneDollarCommon));
    }

    @Override
    protected void bindEvents() {
        mTitleView.setOnTitleBarClickEvent(new TitleView.OnTitleBarClickEvent() {
            @Override
            public void onLeftClick(View view) {
                finish();
            }

            @Override
            public void onTitleClick(View view) {

            }

            @Override
            public void onRightClick(View view) {

            }
        });
    }

    @Override
    protected boolean isNeedLogin() {
        return false;
    }

    @Override
    protected void onLoginStateChange(boolean login) {

    }

    public static void launch(Activity context, String luckyNums, String goodsName, long periodsNum) {
        Intent intent = new Intent(context, LuckyNumDetailActivity.class);
        intent.putExtra(EXTRA_KEY_LUCKY_NUM, luckyNums);
        intent.putExtra(EXTRA_KEY_GOODS_NAME, goodsName);
        intent.putExtra(EXTRA_KEY_PERIODS_NUM, periodsNum);
        context.startActivity(intent);
    }

}
